<?php
namespace app\controllers;

use app\common\corelib\CacheHelper;
use app\logic\CommonLogic;
use app\logic\OrderLogic;
use app\logic\PlayGoodsLogic;
use app\logic\UserLogic;
use app\models\Region;
use Yii;


class PlayGoodsController extends BaseController
{

	public function actionIndex()
	{
            echo "index";
	}
    /**
     * 获取卡券列表
     * @apiDescription   by Jim
     * @api              {post} /play-goods/get-play-list 获取卡券列表
     * @apiName          get-play-list
     * @apiVersion       1.0.0
     * @apiGroup         PlayGoods
     * @apiSampleRequest /play-goods/get-play-list?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * [
     * 'quan':(体验券)
      [{
    `play_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '体验卡id',
    `play_name` varchar(255) NOT NULL COMMENT '体验卡名称',
    `price` decimal(10,2) unsigned NOT NULL COMMENT '价格',
    `type` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '类型：默认0体验券,1体验卡',
    `num` int(10) unsigned NOT NULL COMMENT '体验卡次数',
    `expire_time` int(10) unsigned NOT NULL COMMENT ' 有效时间，天数',
    `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '状态：默认0无效，1有效',

    }]
     'cards':(体验卡)
    [{
    `play_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '体验卡id',
    `play_name` varchar(255) NOT NULL COMMENT '体验卡名称',
    `price` decimal(10,2) unsigned NOT NULL COMMENT '价格',
    `type` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '类型：默认0体验券,1体验卡',
    `num` int(10) unsigned NOT NULL COMMENT '体验卡次数',
    `expire_time` int(10) unsigned NOT NULL COMMENT ' 有效时间，天数',
    `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '状态：默认0无效，1有效',

    }]
     ]
     * @apiError         none
     */
    public function actionGetPlayList()
    {
        $shop_id = $this->getQueryparams('shop_id',0);
        $playGoodsLogic = new PlayGoodsLogic();
        $playList = $playGoodsLogic->getPlayList($shop_id);
        $result = [];
        foreach ($playList AS $key=>$value){
            if($value['type']==0){
                $result['quan'][] = $value;
            }else{
                $result['cards'][] = $value;
            }
        }
        $this->response($result);
    }
    /**
     * 获取商品列表
     * @apiDescription   by Jim
     * @api              {post} /play-goods/get-goods-list 获取商品列表
     * @apiName          get-goods-list
     * @apiVersion       1.0.0
     * @apiParam {string} region_code 精准的位置编码
     * @apiGroup         PlayGoods
     * @apiSampleRequest /play-goods/get-goods-list?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * [{
    `goods_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '商品id',
    `goods_name` varchar(255) NOT NULL COMMENT '商品名称',
    `real_price` decimal(10,2) unsigned NOT NULL COMMENT '价格',
    `price` decimal(10,2) unsigned NOT NULL COMMENT '价格',
    `recovery_num` int(10) unsigned NOT NULL COMMENT '回收充值次数',
    `goods_pic` varchar(255) NOT NULL DEFAULT '' COMMENT '商品主图',
    `goods_pics` text NOT NULL COMMENT '商品轮播图',
    `summary` varchar(255) DEFAULT NULL,
    `effective_days` int(10) unsigned NOT NULL COMMENT '回收有效天数',
    `age` varchar(255) NOT NULL COMMENT '适合年龄',
    `model` varchar(255) NOT NULL COMMENT '型号',
    `description` text,
    `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '状态：默认0无效，1有效',

    }]
     * @apiError         none
     */
    public function actionGetGoodsList()
    {
        $region_code = $this->getQueryparams('region_code','');
        $playGoodsLogic = new PlayGoodsLogic();
        $goodsList = $playGoodsLogic->getGoodsList();
        $stock = $playGoodsLogic->getGoodsStock($region_code);
        $goods_ids = array_column($stock,'goods_id');
        foreach ($goodsList AS $key=>$value){
            if(in_array($value['goods_id'],$goods_ids))
                array_splice($goodsList,$key);
        }
        $this->response($goodsList);
    }
    /**
     * 获取商品详情
     * @apiDescription   by Jim
     * @api              {post} /play-goods/get-goods 获取商品详情
     * @apiName          get-goods
     * @apiVersion       1.0.0
     * @apiGroup         PlayGoods
     * @apiSampleRequest /play-goods/get-goods
     * @apiSuccessExample {json} 获取成功
     * {
    `goods_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '商品id',
    `goods_name` varchar(255) NOT NULL COMMENT '商品名称',
    `real_price` decimal(10,2) unsigned NOT NULL COMMENT '价格',
    `price` decimal(10,2) unsigned NOT NULL COMMENT '价格',
    `recovery_price` decimal(10,2) unsigned NOT NULL COMMENT '价格',
    `goods_pic` varchar(255) NOT NULL DEFAULT '' COMMENT '商品主图',
    `goods_pics` text NOT NULL COMMENT '商品轮播图',
    `summary` varchar(255) DEFAULT NULL,
    `effective_days` int(10) unsigned NOT NULL COMMENT '回收有效天数',
    `age` varchar(255) NOT NULL COMMENT '适合年龄',
    `model` varchar(255) NOT NULL COMMENT '型号',
    `description` text,
    `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '状态：默认0无效，1有效',

    }
     * @apiError         none
     */
    public function actionGetGoods(){
        $goods_id = $this->getQueryparams('goods_id',0);
        $playGoodsLogic = new PlayGoodsLogic();
        $goods = $playGoodsLogic->getGoodss($goods_id);
        $goods['order_time'] = date('Y-m-d');
        $goods['expire_time'] = date('Y-m-d', strtotime("+".$goods['effective_days']." day"));
        $this->response($goods);
    }
    /**
     * 获取体验卡
     * @apiDescription   by Jim
     * @api              {post} /play-goods/get-play-card 获取体验卡
     * @apiName          get-play-card
     * @apiVersion       1.0.0
     * @apiParam {string} access_token
     * @apiGroup         PlayGoods
     * @apiSampleRequest /play-goods/get-play-card?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * {
    `order_id` int(11) NOT NULL AUTO_INCREMENT,
    `order_no` varchar(255) NOT NULL COMMENT '订单编号',
    `type` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '订单类型：默认0，play类型，1,goods类型',
    `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '订单状态：0未支付,1已付款，2,已提货，3使用中，4已使用或已回收',
    `gp_id` int(11) unsigned NOT NULL COMMENT '商品或者体验券id',
    `current_num` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '体验卡或者体验券当前有效次数，商品为0，体验券为0或者1，体验卡为0+',
    `order_time` datetime NOT NULL COMMENT '下单时间',
    `pay_time` datetime DEFAULT NULL COMMENT '支付时间',
    `expire_time` date NOT NULL COMMENT '有效时间',
    `user_id` int(11) unsigned NOT NULL COMMENT ' 购买用户id',
    `shop_id` int(11) unsigned DEFAULT NULL COMMENT '自提点商店',

    }
     * @apiError         400001 请去充值
     */
    public function actionGetPlayCard(){
        $userLogic = new UserLogic();
        $orderLogic = new OrderLogic();
        $user = $userLogic->getUser($this->user_id);
        if($user->current_num>0){
            $order = $orderLogic->getLatestPlayCard($this->user_id);
            $order['total_num'] = $user->current_num;
            $this->response($order);
        }else{
            $this->response(['code'=>'400001','msg'=>'请去充值']);
        }
    }



}